package com.opengl.textprint.utils;

import android.util.Log;

import kotlin.text.Regex;

/**
 * Author: Wu Chang
 * 部分HTML在textView显示异常需要单独的处理
 * Date: 2023/7/19 16:29
 */
public class HtmlResetUtil {

    private static final String TAG = "HtmlResetUtil";

    /**
     * 颜色的正则
     */
    private static final String COLOR_RGB = "(?<=style=\\\".{0,100})color: ?rgb\\(\\d{0,3}, ?\\d{0,3}, ?\\d{0,3}\\)(?=.{0,100}\">)";

    /**
     * 右对齐的正则
     */
    private static final String TEXT_ALIGN_RIGHT = "(?<=style=\\\".{0,100})text-align: ?right(?=.{0,100}\">)";

    /**
     * 移除P标签，并使用</br>替换</p>
     */
    private static final String TEXT_REMOVE_P = "<p ?style=\".{0,50}?\">";

    /**
     * 这个颜色Android不识别 color: rgb(255, 163, 32) 需要使用 color: #ffa320
     *
     * @param resMsg html
     * @return html
     */
    public static String checkFontColor(String resMsg) {
        return replaceColorRgb(resMsg);
    }

    private static String replaceColorRgb(String readMsg) {
        Regex regex = new Regex(COLOR_RGB);
        StringBuilder builder = new StringBuilder();
        final int[] index = new int[1];
        regex.findAll(readMsg, 0).iterator().forEachRemaining(result -> {
            String value = result.getValue();
            int start = result.getRange().getStart();
            int end = start + value.length();
            Log.d(TAG, "GameReadItemHolder  index = " + value);
            builder.append(readMsg.substring(index[0], start));
            int rgbStart = value.indexOf("(");
            int rgbEnd = value.indexOf(")");
            String rgb = value.substring(rgbStart + 1, rgbEnd);
            Log.d(TAG, "rgb " + rgb);
            String[] ffg = rgb.split(",");
            String fg = intToHex(ffg[0]);
            String fg1 = intToHex(ffg[1]);
            String fg2 = intToHex(ffg[2]);
            builder.append("color: #").append(fg).append(fg1).append(fg2);
            index[0] = end;
        });
        if (index[0] == 0) {
            return readMsg;
        }
        builder.append(readMsg.substring(index[0]));
        return builder.toString();
    }

    /**
     * int 类型的String字符串转换成 16进制的字符串
     *
     * @param value v
     * @return 255->ff, 1->01, 15-> 0f
     */
    private static String intToHex(String value) {
        StringBuilder fg = new StringBuilder(Integer.toHexString(Integer.parseInt(value.trim())));
        while (fg.length() < 2) {
            fg.insert(0, "0");
        }
        return fg.toString();
    }

    /**
     * text-align: right 不支持，需要改成 text-align: end
     *
     * @param readMsg msg
     * @return end
     */
    public static String textAlignRight(String readMsg) {
        Regex regex = new Regex(TEXT_ALIGN_RIGHT);
        StringBuilder builder = new StringBuilder();
        final int[] index = new int[]{0};
        regex.findAll(readMsg, 0).iterator().forEachRemaining(result -> {
            String value = result.getValue();
            int start = result.getRange().getStart();
            builder.append(readMsg.substring(index[0], start));
            builder.append("text-align: end");
            index[0] = start + value.length();
        });
        if (index[0] == 0) {
            return readMsg;
        }
        builder.append(readMsg.substring(index[0]));
        return builder.toString();
    }

    /**
     * 将P标签移除，</br>取代</p>
     * @param msg msg
     * @return msg
     */
    public static String textPToBr(String msg) {
        Regex regex = new Regex(TEXT_REMOVE_P);
        StringBuilder builder = new StringBuilder();
        final int[] index = new int[]{0};
        regex.findAll(msg, 0).iterator().forEachRemaining(result -> {
            int start = result.getRange().getStart();
            String value = result.getValue();
            if (start != index[0]) {
                String pv = msg.substring(index[0], start);
                builder.append(pv);
            }
            index[0] = start + value.length();
        });
        if (index[0] == 0) {
            return msg.replaceAll("</p>", "<br>");
        }
        builder.append(msg.substring(index[0]));
        String result = builder.toString();
        return result.replaceAll("</p>", "<br>");
    }

    /**
     * 去除段落的默认margin（编辑器自动添加的，导致前端展示的数据格式与编辑器不一致）
     * @param text
     * @return
     */
    public static String removePMargin(String text) {
        return text.replaceAll("<p style=\"min-height: 16px;\"></p>", "<br>");
    }
}
